package com.nft.qz.reqip.ipcity;

import com.ip2location.IP2Location;
import com.ip2location.IPResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class IPLocationFinder {
    @Value("${ip.db2}")
    private String dbPath;

    public Map<String, String> parseIp(String ip) {
        Map<String, String> map = new HashMap<>();
        try {
            IP2Location ip2Location = new IP2Location();
            ip2Location.Open(dbPath);
            IPResult ipResult = ip2Location.IPQuery(ip);
            float latitude = ipResult.getLatitude();
            float longitude = ipResult.getLongitude();
            map.put("latitude", String.valueOf(latitude));
            map.put("longitude", String.valueOf(longitude));
        } catch (Exception e) {
            map.put("latitude", "");
            map.put("longitude", "");
        }
        return map;
    }
}